<template>
  <div class="box" v-if="article != null">
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ article.title }}</h1>

      <!-- 用户信息 -->
      <van-cell
        center
        :title="article.aut_name"
        :label="article.pubdate | dateFormat"
      >
        <template #icon>
          <!-- 头像 -->
          <img :src="article.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <!-- 是否关注了作者 -->
            <van-button
              type="info"
              size="mini"
              v-if="article.is_followed"
              @click="unfollowUser"
              >已关注</van-button
            >
            <van-button
              icon="plus"
              type="info"
              size="mini"
              plain
              v-else
              @click="followUser"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="article.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          icon="good-job"
          type="danger"
          size="small"
          v-if="article.attitude === 1"
          @click="dislikeArticle"
          >已点赞</van-button
        >
        <van-button
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          v-else
          @click="likeArticle"
          >点赞</van-button
        >
      </div>
      <ArtCmt :art-id="id"></ArtCmt>
    </div>
  </div>
</template>

<script>
import { getArticleDetailAPI, followUserAPI, unfollowUserAPI, likeArticleAPI, deslikeArticleAPI } from '../../api/articleAPI'
import { Toast } from 'vant'
import ArtCmt from '../ArtCmt/ArtCmt.vue'
export default {
  name: 'ArticleDetail',
  props: ['id'],
  components: {
    ArtCmt
  },
  data () {
    return {
      article: null
    }
  },
  created () {
    this.initArticleInfo()
  },
  methods: {
    async initArticleInfo () {
      const { data: res } = await getArticleDetailAPI(this.id)
      if (res.message === 'OK') {
        this.article = res.data
      }
    },
    async followUser () {
      const { data: res } = await followUserAPI(this.article.aut_id)
      if (res.message === 'OK') {
        Toast.success('关注成功')
        this.article.is_followed = true
      }
    },
    async unfollowUser () {
      const res = await unfollowUserAPI(this.article.aut_id)
      if (res.status === 204) {
        Toast.success('取关成功')
        this.article.is_followed = false
      }
    },
    async likeArticle () {
      const { data: res } = await likeArticleAPI(this.article.art_id)
      if (res.message === 'OK') {
        Toast.success('点赞成功')
        this.article.attitude = 1
      }
    },
    async dislikeArticle () {
      const res = await deslikeArticleAPI(this.article.art_id)
      if (res.status === 204) {
        Toast.success('取消点赞')
        this.article.attitude = -1
      }
    }
  }
}
</script>

<style lang="less" scoped>
.box {
  padding-bottom: 70px;
}
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
